package org.example.user.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.user.domain.RoleMenu;
import org.example.user.domain.User;
import org.example.user.domain.UserRole;
import org.example.user.service.RoleMenuService;
import org.example.user.service.UserRoleService;
import org.example.user.service.UserService;
import org.example.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 26351
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2024-05-21 20:28:46
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Override
    public List<User> listAll(User user) {
        return baseMapper.listAll(user);
    }

    @Override
    @Transactional
    public boolean saveAll(User user) {
        user.setSalt("qwer");
        String pwd = DigestUtil.md5Hex(user.getPassword() + "qwer");
        user.setPassword(pwd);
        user.setCreateDate(DateUtil.now());
        boolean save = this.save(user);
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(user.getRoleId());
        save=userRoleService.save(userRole);
        return save;
    }

    @Override
    @Transactional
    public boolean updateAll(User user) {
        boolean b = this.updateById(user);
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId,user.getId());
        UserRole byId = userRoleService.getOne(wrapper);
        byId.setRoleId(user.getRoleId());
        b = userRoleService.updateById(byId);
        return b;
    }

    @Override
    public User getUserByUserName(String username) {
        return baseMapper.getUserByUserName(username);
    }
}




